package com.dragon.english_practice_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.english_practice_back.entity.Grammars;
import com.dragon.english_practice_back.service.AuthenticationUtil;
import com.dragon.english_practice_back.service.GrammarsService;
import com.dragon.english_practice_back.mapper.GrammarsMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author 伫倚
* @description 针对表【grammars】的数据库操作Service实现
* @createDate 2025-04-15 15:28:40
*/
@Service
@AllArgsConstructor
public class GrammarsServiceImpl extends ServiceImpl<GrammarsMapper, Grammars>
    implements GrammarsService{

    AuthenticationUtil authenticationUtil;
    GrammarsMapper grammarsMapper;
    @Override
    public Page<Map<String, Object>> getGrammars(Page<Map<String, Object>> page) {
        Page<Map<String, Object>> pageResult = new Page<>(page.getCurrent(), page.getSize());
        pageResult.setTotal(grammarsMapper.selectCount(new QueryWrapper<>()));
        pageResult.setPages(pageResult.getTotal() / pageResult.getSize() + 1);

        //取出order信息
        List<OrderItem> orders = page.getOrders();
        //取出orderBy信息
        String orderBy = "";
        if (!orders.isEmpty()) {
            orderBy = orders.get(0).getColumn();
        }
        //取出order信息
        String order = "";
        if (!orders.isEmpty()) {
            order = orders.get(0).isAsc() ? "asc" : "desc";
        }

        List<Map<String, Object>> grammars = grammarsMapper.getGrammarsWithProficiency((page.getCurrent()-1)*page.getSize(), page.getSize(), authenticationUtil.id(), orderBy, order);
        pageResult.setOrders(orders);
        pageResult.setRecords(grammars);
        return pageResult;
    }
}




